#ifndef __CMD_H__
#define __CMD_H__

#include "minigui/common.h"

typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;

#define MAX_DATA_SIZE       1024
#define SEND_BUF_SIZE       1500
#define RECV_BUF_SIZE       512

#define SYS_STATE_READY     1
#define SYS_STATE_RESET     2
#define SYS_STATE_KNIT      3
#define SYS_STATE_HALT      4
#define SYS_STATE_PK        5

#define CMD_SIGNATURE   ('M' | ('H' << 8))
#define CMD_SIGNATURE0  'M'
#define CMD_SIGNATURE1  'H'
#define CMD_EOF         '.'

#define CMD_QUERY_SYS_STATE     0x8001
#define CMD_QUERY_FB_IN_RUN     0x8002
#define CMD_QUERY_VERSION       0x8003
#define CMD_QUERY_FB_IN_TEST    0x8004
#define CMD_QUERY_FB_IN_CTEST   0x8005
//////////////////////////////////////////////////
#define CMD_QUERY_SYS_TIME              0x8006
#define CMD_QUERY_SYS_ID                    0x8007
#define CMD_QUERY_FB_IN_CTEST_RC           0x8008
#define CMD_QUERY_COMB_FB               0x8009
#define CMD_QUERY_UPGRADE_JIJIA         0x800a


/* 复位 */
#define CMD_REQUEST_RESET       0x4001
/* 纱嘴收起 */
#define CMD_REQUEST_YC_UP       0x4002
/* 送纱器 */
#define CMD_REQUEST_YF          0x4003
/* 日光灯 */
#define CMD_REQUEST_DL          0x4004
/* 摇床 */
#define CMD_REQUEST_ROCK        0x4005
/* 机头 */
#define CMD_REQUEST_CARRIAGE    0x4006
/* CAN消息转发 */
#define CMD_REQUEST_CAN         0x4007
/* 信号灯 */
#define CMD_REQUEST_SIGLIGHT    0x4008
/* 低罗拉 */
#define CMD_REQUEST_LOWROLLER           0x4009
/* 高罗拉 */
#define CMD_REQUEST_HIGHROLLER          0x400A
/* 跳行编织 */
#define CMD_REQUEST_SET_SYS_STATE       0x400B
/* 行定 */
#define CMD_REQUEST_LOCKROW             0x400C
/* 指定行停车 */
#define CMD_REQUEST_STOPLINE            0x400D
/* 片锁 */
#define CMD_REQUEST_PIECELOCK           0x400E
//////////////////////////////////////////////////
/* 速度设置 */
#define CMD_REQUEST_SPEED                   0x400F
/* 设置系统时间 */
#define CMD_REQUEST_SET_SYS_TIME    0x4010
/* 设置机型 */
#define CMD_REQUEST_SET_MAC_TYPE    0x4011
/* 保存数据 */
#define CMD_REQUEST_SAVE_DATA            0x4012
/* 机架板升级请求 */
#define CMD_REQUEST_UPGRADE_JIJIA    0x4013
/* can长数据转发 */
#define CMD_REQUEST_LONG_CAN            0x4014
/* 机架板复位请求*/
#define CMD_REQUEST_RESET_JIJIA    0x4016

#define CMD_MODIFY_WORKPRM      0x2001
#define CMD_MODIFY_SYSPRM       0x2002
#define CMD_MODIFY_MACPARAM     0x2003
#define CMD_MODIFY_STITCHAMEND  0x2004
#define CMD_MODIFY_YCPARKPOS    0x2005
#define CMD_MODIFY_SPEED        0x2006
#define CMD_MODIFY_STITCH       0x2007
#define CMD_MODIFY_PAT          0x2008
#define CMD_MODIFY_CNT          0x2009
#define CMD_MODIFY_SERVO        0x200A
#define CMD_MODIFY_MULTICLOTH   0x200B
#define CMD_MODIFY_ROCK         0x200C
#define CMD_MODIFY_CLOTHNUM     0x200D
#define CMD_MODIFY_YARNBEFOR    0x200E
#define CMD_MODIFY_CHANGEYARN   0x200F
#define CMD_MODIFY_CHANGESEC    0x2010
#define CMD_MODIFY_ALARMMASK    0x2011
#define CMD_MODIFY_PK_CON       0x2012
#define CMD_MODIFY_PK_DAT       0x2013
//////////////////////////////////////////////////////////////////
#define CMD_MODIFY_HIDEPRM          0x2014
#define CMD_MODIFY_SINK             0x2015
#define CMD_MODIFY_YARNFEEDER       0x2016
#define CMD_MODIFY_JIJIA            0x2017
#define CMD_MODIFY_COMBPARAM        0x2018
#define CMD_MODIFY_CAMSETTING       0x2019

typedef struct _MODIFY MODIFY;

typedef struct _REQUEST_RESET            REQUEST_RESET;
typedef struct _REQUEST_YC_UP            REQUEST_YC_UP;
typedef struct _REQUEST_YF               REQUEST_YF;
typedef struct _REQUEST_DL               REQUEST_DL;
typedef struct _REQUEST_ROCK             REQUEST_ROCK;
typedef struct _REQUEST_CARRIAGE         REQUEST_CARRIAGE;
typedef struct _REQUEST_CAN              REQUEST_CAN;
typedef struct _REQUEST_SIGLIGHT         REQUEST_SIGLIGHT;
typedef struct _REQUEST_HIGHROLLER       REQUEST_HIGHROLLER;
typedef struct _REQUEST_LOWROLLER        REQUEST_LOWROLLER;
typedef struct _REQUEST_SET_SYS_STATE    REQUEST_SET_SYS_STATE;
typedef struct _REQUEST_LOCKROW          REQUEST_LOCKROW;
typedef struct _REQUEST_STOPLINE         REQUEST_STOPLINE;
typedef struct _REQUEST_PIECELOCK        REQUEST_PIECELOCK;
typedef struct _REQUEST_SET_MAC_TYPE  REQUEST_SET_MAC_TYPE;
typedef struct _REQUEST_MACSPEED          REQUEST_MACSPEED;
typedef struct _REQUEST_SETTIME             REQUEST_SETTIME;
typedef struct _REQUEST_MACTIME            REQUESET_MACTIME;
typedef struct _REQUEST_MACTYPE             REQUESET_MACTYPE;
typedef struct _REQUEST_SAVEDATA            REQUESET_SAVEDATA;
typedef struct _REQUEST_UPGRADE             REQUEST_UPGRADE;

typedef struct _SENSOR_LIST SENSOR_LIST;

#pragma pack(1)

struct _SENSOR_LIST
{
    BYTE interface;
    BYTE datebyte;
    BYTE datebit;
};

struct _MODIFY
{
    ushort cmd;
    uint total_size;
    ushort size;
    uint offset;
    uchar *content;
};

struct _REQUEST_RESET
{
    ushort cmd;
    ushort nRow;
    short yc_pos[16];
    uchar reset_type;       /* 0-从编织文件开始编织；1-从前缀编织开始 */
};

struct _REQUEST_SET_SYS_STATE
{
    ushort cmd;
    ushort sys_state;
};

struct _REQUEST_YF
{
    ushort cmd;
    uchar side;
    uchar run;      /* 0, stop; 1, run */
};

struct _REQUEST_YC_UP
{
    ushort cmd;
    uchar dir;      /* 0-收起；1-打下 */
};

struct _REQUEST_DL
{
    ushort cmd;
    uchar power;    /* 0, off; 1, on */
};

struct _REQUEST_ROCK
{
    ushort cmd;
    uchar type;     /* 0, reset; 1, act */
    short pos;
};

struct _REQUEST_CARRIAGE
{
    ushort cmd;
    uchar type;     /* 0-停止，1-左行，2-右行 */
    uchar speed;    /* 1-100 */
};

struct _REQUEST_CAN
{
    ushort cmd;
    ushort id;
    ushort len;
    uchar data[8];
};

struct _REQUEST_SIGLIGHT
{
    ushort cmd;
    BYTE green;     /* 0 - off, 1 - on*/
    BYTE yellow;
    BYTE red;
    BYTE buzzer;
};

struct _REQUEST_HIGHROLLER
{
    ushort cmd;
    BYTE speed;
    int pulse;
};

struct  _REQUEST_LOWROLLER
{
    ushort cmd;
    BYTE speed;
};

struct _REQUEST_SET_MAC_TYPE
{
    ushort cmd;
    short sys_type;
    short mac_type;
};

struct _REQUEST_JUMPROW
{
    ushort cmd;
    ushort nRow;
};

struct _REQUEST_LOCKROW
{
    ushort cmd;
    short start_row;
    short end_row;
    short times;
};

struct _REQUEST_STOPLINE
{
    ushort cmd;
    ushort nRow;    /*0 -- 无效；Ｘ -- 行数*/
};

struct _REQUEST_PIECELOCK
{
    ushort cmd;
    ushort lock;
};

struct _REQUEST_MACSPEED
{
    ushort cmd;
    uchar speed;
};

struct _REQUEST_SETTIME
{
    ushort cmd;
    int set_time;
    int add_time;
    short lock;
};

struct _REQUEST_MACTYPE
{
    ushort cmd;
    ushort sys_type;
    ushort mac_type;
};

struct _REQUEST_SAVEDATA
{
    ushort cmd;
    ushort data;
};

struct _REQUEST_UPGRADE
{
    ushort cmd;
    ushort data;
};

#pragma pack()

typedef struct _RESPONSE                RESPONSE;
typedef struct _QUERY_SYS_STATE_RESP    QUERY_SYS_STATE_RESP;
typedef struct _QUERY_FB_IN_TEST_RESP   QUERY_FB_IN_TEST_RESP;
typedef struct _QUERY_FB_IN_RUN_RESP    QUERY_FB_IN_RUN_RESP;
typedef struct _QUERY_FB_IN_CTEST_RESP  QUERY_FB_IN_CTEST_RESP;
typedef struct _QUERY_VERSION           QUERY_VERSION;
typedef struct _QUERY_UPGRADE_JIJIA QUERY_UPGRADE_JIJIA;
typedef struct _QUERY_SYS_TIME      QUERY_SYS_TIME;
typedef struct _QUERY_SYS_ID         QUERY_SYS_ID;


#pragma pack(1)
struct _RESPONSE
{
    ushort type;
    ushort state;
};

#define STATE_RESET_YC          1
#define STATE_RESET_CAM         2
#define STATE_RESET_SEL         3
#define STATE_RESET_STITCH      4
#define STATE_RESET_SINK        5
#define STATE_RESET_CARRIAGE    6
#define STATE_RESET_ROCK        7
#define STATE_RESET_COMPLETE    8

struct _QUERY_SYS_STATE_RESP
{
    RESPONSE resp;
    ushort reset_state;     /* 复位状态
                               1-纱嘴复位
                               2-三角复位
                               3-选针器复位
                               4-度目复位
                               5-辛克复位
                               6-机头复位
                               7-摇床复位
                               8-复位完成 */
};

struct _QUERY_FB_IN_TEST_RESP
{
    RESPONSE resp;
    short rpos;
    short needle_num;
    uchar sensor_state[6];
    int plus_num;
    BYTE mm_state;
    BYTE rock_state;
};

struct _QUERY_FB_IN_RUN_RESP
{
    RESPONSE resp;
    ushort errcode;
    char mm_state;
    char rock_state;
    short needle_num;
    short row;
    short cycle_start;
    short cycle_end;
    short cycle_times;
    short cloth_finished;
    char bar_state;
};

struct _QUERY_FB_IN_CTEST_RESP
{
    RESPONSE resp;
    int cerror;
    short sensor;               //度目电机感应器状态
    char yc_state[2];
    char cam_state[4];      /*  0-左系统前板
                                1-左系统后板
                                2-右系统前板
                                3-右系统后板　*/

    char sel_state[8];      /*  0-选针器前１
                                1-选针器前２
                                2-选针器后１
                                3-选针器后２
                                4-选针器前３
                                5-选针器前４
                                6-选针器后３
                                7-选针器后４ */

    short st_pos[10];       /*　 0-度目前１位置
                                1-度目前２位置
                                2-度目后１位置
                                3-度目后２位置
                                4-前辛克位置
                                5-后辛克位置
                                6-度目前３位置
                                7-度目前４位置
                                8-度目后３位置
                                9-度目后４位置 */
    short cam_pos[4]; /*    1-三角电机前1位置
                                                2-三角电机后1位置
                                                3- 三角电机前2位置
                                                4-三角电机后2位置
                                            */
    short sink_pos[2];/*    1-前辛克2位置
                                                2-后辛克2位置
                                            */
    short sensor2;  //机头感应器状态
    int cerror2;

};

struct _QUERY_VERSION
{
    RESPONSE resp;
    ushort rack;            /* 机架板版本号 */
    ushort rack_bl;         /* 机架板bootloader版本号 */
    ushort carriage;        /* 机头板版本号 */
    ushort carriage_bl;     /* 机头板bootloader版本号 */
    ushort comb;            /* 起底板版本号 */
    ushort comb_bl;         /* 起底板bootloader版本号 */
    ushort motor;           /* 电机板版本号 */
    ushort motor_bl;        /* 电机板bootloader版本号 */
};

struct _QUERY_UPGRADE_JIJIA
{
    RESPONSE resp;
    uchar state;
    uchar percent;
    uchar error;
};

struct _QUERY_SYS_TIME
{
    RESPONSE resp;
    int time_set;
    int time_add;
    ushort lock;
};

struct _QUERY_SYS_ID
{
    RESPONSE resp;
    char id[8];
};

#pragma pack()

#include <mh/common.h>

unsigned short checksum(unsigned short *data, unsigned short size);
int cmd_query(MH_UDP *pudp, uchar *query, int query_size, RESPONSE *resp, int resp_size);
int cmd_modify(MH_UDP *pudp, MODIFY *pmodify);
int cmd_request(MH_UDP *pudp, uchar *request, int size);
int cmd_request_just(MH_UDP *pudp, uchar *request, int size);
extern uchar send_buf[SEND_BUF_SIZE];
extern uchar recv_buf[RECV_BUF_SIZE];

#endif /* __CMD_H__ */
